<template>
	<el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
		<el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
			label-width="80px">
			<el-form-item label="用户id" prop="userId">
				<el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
			</el-form-item>
			<el-form-item label="文章内容" prop="content">
				<el-input v-model="dataForm.content" placeholder="文章内容"></el-input>
			</el-form-item>
			<el-form-item label="文章类型（0 文章 1 动态）" prop="type">
				<el-input v-model="dataForm.type" placeholder="文章类型（0 文章 1 动态）"></el-input>
			</el-form-item>
			<el-form-item label="地理位置" prop="location">
				<el-input v-model="dataForm.location" placeholder="地理位置"></el-input>
			</el-form-item>
			<el-form-item label="原创作品" prop="isOriginal">
				<el-input v-model="dataForm.isOriginal" placeholder="原创作品"></el-input>
			</el-form-item>
			<el-form-item label="能否点赞" prop="isLike">
				<el-input v-model="dataForm.isLike" placeholder="能否点赞"></el-input>
			</el-form-item>
			<el-form-item label="能否收藏" prop="isCollection">
				<el-input v-model="dataForm.isCollection" placeholder="能否收藏"></el-input>
			</el-form-item>
			<el-form-item label="能否转发" prop="isForward">
				<el-input v-model="dataForm.isForward" placeholder="能否转发"></el-input>
			</el-form-item>
			<el-form-item label="经度" prop="longitude">
				<el-input v-model="dataForm.longitude" placeholder="经度"></el-input>
			</el-form-item>
			<el-form-item label="维度" prop="dimension">
				<el-input v-model="dataForm.dimension" placeholder="维度"></el-input>
			</el-form-item>
			<el-form-item label="文章生成二维码" prop="qrCode">
				<el-input v-model="dataForm.qrCode" placeholder="文章生成二维码"></el-input>
			</el-form-item>
			<el-form-item label="转发次数" prop="forwardCount">
				<el-input v-model="dataForm.forwardCount" placeholder="转发次数"></el-input>
			</el-form-item>
			<el-form-item label="收藏次数" prop="collectionCount">
				<el-input v-model="dataForm.collectionCount" placeholder="收藏次数"></el-input>
			</el-form-item>
			<el-form-item label="查看数量" prop="viewCount">
				<el-input v-model="dataForm.viewCount" placeholder="查看数量"></el-input>
			</el-form-item>
			<el-form-item label="评论次数" prop="commentCount">
				<el-input v-model="dataForm.commentCount" placeholder="评论次数"></el-input>
			</el-form-item>
			<el-form-item label="点赞次数" prop="likeCount">
				<el-input v-model="dataForm.likeCount" placeholder="点赞次数"></el-input>
			</el-form-item>
			<el-form-item label="能否出版发布" prop="isPublish">
				<el-input v-model="dataForm.isPublish" placeholder="能否出版发布"></el-input>
			</el-form-item>
			<el-form-item label="文章来源" prop="pid">
				<el-input v-model="dataForm.pid" placeholder="文章来源"></el-input>
			</el-form-item>
			<el-form-item label="话题id" prop="topicId">
				<el-input v-model="dataForm.topicId" placeholder="话题id"></el-input>
			</el-form-item>
			<el-form-item label="分类id" prop="labelId">
				<el-input v-model="dataForm.labelId" placeholder="分类id"></el-input>
			</el-form-item>
			<el-form-item label="启用状态" prop="status">
				<el-input v-model="dataForm.status" placeholder="启用状态"></el-input>
			</el-form-item>
			<el-form-item label="创建时间" prop="createTime">
				<el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
			</el-form-item>
			<el-form-item label="更新时间" prop="updateTime">
				<el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
			</el-form-item>
		</el-form>
		<span slot="footer" class="dialog-footer">
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="dataFormSubmit()">确定</el-button>
		</span>
	</el-dialog>
</template>

<script>
	export default {
		data() {
			return {
				visible: false,
				dataForm: {
					id: 0,
					userId: '',
					content: '',
					type: '',
					location: '',
					isOriginal: '',
					isLike: '',
					isCollection: '',
					isForward: '',
					longitude: '',
					dimension: '',
					qrCode: '',
					forwardCount: '',
					collectionCount: '',
					viewCount: '',
					commentCount: '',
					likeCount: '',
					isPublish: '',
					pid: '',
					topicId: '',
					labelId: '',
					status: '',
					createTime: '',
					updateTime: ''
				},
				dataRule: {
					userId: [{
						required: true,
						message: '用户id不能为空',
						trigger: 'blur'
					}],
					content: [{
						required: true,
						message: '文章内容不能为空',
						trigger: 'blur'
					}],
					type: [{
						required: true,
						message: '文章类型（0 文章 1 动态）不能为空',
						trigger: 'blur'
					}],
					location: [{
						required: true,
						message: '地理位置不能为空',
						trigger: 'blur'
					}],
					isOriginal: [{
						required: true,
						message: '原创作品不能为空',
						trigger: 'blur'
					}],
					isLike: [{
						required: true,
						message: '能否点赞不能为空',
						trigger: 'blur'
					}],
					isCollection: [{
						required: true,
						message: '能否收藏不能为空',
						trigger: 'blur'
					}],
					isForward: [{
						required: true,
						message: '能否转发不能为空',
						trigger: 'blur'
					}],
					longitude: [{
						required: true,
						message: '经度不能为空',
						trigger: 'blur'
					}],
					dimension: [{
						required: true,
						message: '维度不能为空',
						trigger: 'blur'
					}],
					qrCode: [{
						required: true,
						message: '文章生成二维码不能为空',
						trigger: 'blur'
					}],
					forwardCount: [{
						required: true,
						message: '转发次数不能为空',
						trigger: 'blur'
					}],
					collectionCount: [{
						required: true,
						message: '收藏次数不能为空',
						trigger: 'blur'
					}],
					viewCount: [{
						required: true,
						message: '查看数量不能为空',
						trigger: 'blur'
					}],
					commentCount: [{
						required: true,
						message: '评论次数不能为空',
						trigger: 'blur'
					}],
					likeCount: [{
						required: true,
						message: '点赞次数不能为空',
						trigger: 'blur'
					}],
					isPublish: [{
						required: true,
						message: '能否出版发布不能为空',
						trigger: 'blur'
					}],
					pid: [{
						required: true,
						message: '文章来源不能为空',
						trigger: 'blur'
					}],
					topicId: [{
						required: true,
						message: '话题id不能为空',
						trigger: 'blur'
					}],
					labelId: [{
						required: true,
						message: '分类id不能为空',
						trigger: 'blur'
					}],
					status: [{
						required: true,
						message: '启用状态不能为空',
						trigger: 'blur'
					}],
					createTime: [{
						required: true,
						message: '创建时间不能为空',
						trigger: 'blur'
					}],
					updateTime: [{
						required: true,
						message: '更新时间不能为空',
						trigger: 'blur'
					}]
				}
			}
		},
		methods: {
			init(id) {
				this.dataForm.id = id || 0
				this.visible = true
				this.$nextTick(() => {
					this.$refs['dataForm'].resetFields()
					if (this.dataForm.id) {
						this.request({
							url: `/api/tweet/info/${this.dataForm.id}`,
							method: 'get',
							params: {}
						}).then((res) => {
							var result = res.result
							if (res && res.code == 200) {
								this.dataForm.userId = result.userId
								this.dataForm.content = result.content
								this.dataForm.type = result.type
								this.dataForm.location = result.location
								this.dataForm.isOriginal = result.isOriginal
								this.dataForm.isLike = result.isLike
								this.dataForm.isCollection = result.isCollection
								this.dataForm.isForward = result.isForward
								this.dataForm.longitude = result.longitude
								this.dataForm.dimension = result.dimension
								this.dataForm.qrCode = result.qrCode
								this.dataForm.forwardCount = result.forwardCount
								this.dataForm.collectionCount = result.collectionCount
								this.dataForm.viewCount = result.viewCount
								this.dataForm.commentCount = result.commentCount
								this.dataForm.likeCount = result.likeCount
								this.dataForm.isPublish = result.isPublish
								this.dataForm.pid = result.pid
								this.dataForm.topicId = result.topicId
								this.dataForm.labelId = result.labelId
								this.dataForm.status = result.status
								this.dataForm.createTime = result.createTime
								this.dataForm.updateTime = result.updateTime
							}
						})
					}
				})
			},
			// 表单提交
			dataFormSubmit() {
				this.$refs['dataForm'].validate((valid) => {
					if (valid) {
						this.request({
							url: `/api/tweet/${!this.dataForm.id ? 'save' : 'update'}`,
							method: 'post',
							data: {
								'id': this.dataForm.id || undefined,
								'userId': this.dataForm.userId,
								'content': this.dataForm.content,
								'type': this.dataForm.type,
								'location': this.dataForm.location,
								'isOriginal': this.dataForm.isOriginal,
								'isLike': this.dataForm.isLike,
								'isCollection': this.dataForm.isCollection,
								'isForward': this.dataForm.isForward,
								'longitude': this.dataForm.longitude,
								'dimension': this.dataForm.dimension,
								'qrCode': this.dataForm.qrCode,
								'forwardCount': this.dataForm.forwardCount,
								'collectionCount': this.dataForm.collectionCount,
								'viewCount': this.dataForm.viewCount,
								'commentCount': this.dataForm.commentCount,
								'likeCount': this.dataForm.likeCount,
								'isPublish': this.dataForm.isPublish,
								'pid': this.dataForm.pid,
								'topicId': this.dataForm.topicId,
								'labelId': this.dataForm.labelId,
								'status': this.dataForm.status,
								'createTime': this.dataForm.createTime,
								'updateTime': this.dataForm.updateTime
							}
						}).then(({
							data
						}) => {
							if (data && code === 0) {
								this.$message({
									message: '操作成功',
									type: 'success',
									duration: 1500,
									onClose: () => {
										this.visible = false
										this.$emit('refreshDataList')
									}
								})
							} else {
								this.$message.error(msg)
							}
						})
					}
				})
			}
		}
	}
</script>
